package com.wake.mycomposebook.ui.home.list.my.vertical

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material3.Icon
import androidx.compose.material3.IconToggleButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.wake.data.DemoDataProvider
import com.wake.data.model.Item

/**
 *@author created by WA03675
 *@date  2022-04-14 17:02
 *@des
 */
/**
 * 如果进行分类
 *用position?
 */
@Composable
fun MyVerticalList() {
    val list = remember {
        DemoDataProvider.itemList
    }

    LazyColumn {
        items(list, key = { it.id }) {
            MyVerticalListItem(item = it)
        }
    }
}

@Composable
fun MyVerticalListItem(item: Item) {
    when (item.id % 4) {
        1, 2 -> {
            VerticalItem1(item)
        }
        3 -> HorizontalItem(item = item)
        else -> {}
    }
    ListLine()
}

@Composable
fun VerticalItem1(item: Item) {
    Column(
        Modifier.padding(0.dp, 10.dp)
    ) {
        Image(
            painter = painterResource(id = item.imageId), modifier = Modifier
                .padding(15.dp)
                .height(150.dp)
                .fillMaxWidth(),
            contentScale = ContentScale.Crop,
            contentDescription = null
        )
        Text(text = item.title, style = MaterialTheme.typography.titleMedium)
        Text(text = item.subtitle, style = androidx.compose.material.MaterialTheme.typography.body2)
        Text(text = item.source, style = MaterialTheme.typography.bodySmall)

    }
}

@Composable
fun HorizontalItem(item: Item) {
    val favorite = remember {
        mutableStateOf(false)
    }

    Row(
        verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(0.dp, 10.dp)
    ) {
        ListImageItem(
            item = item, modifier = Modifier
                .padding(10.dp)
                .width(80.dp)
                .height(60.dp)
        )


        Column(modifier = Modifier.weight(1f)) {
            Text(text = item.title, style = MaterialTheme.typography.titleMedium)
            Text(text = item.subtitle, style = androidx.compose.material.MaterialTheme.typography.body2)

        }
//        androidx.compose.material3.Surface(
//            modifier = Modifier
//                .background(Color.Transparent, CircleShape)
//                .width(30.dp)
//                .height(30.dp),
//        ) {
//            Icon(
//                imageVector = if (favorite.value) Icons.Filled.FavoriteBorder else Icons.Filled.Favorite,
//                contentDescription = null,
//                modifier = Modifier
//                    .clickable {
//                        favorite.value = !favorite.value
//                    }
//            )
//        }
        IconToggleButton(checked = favorite.value, onCheckedChange = {
            favorite.value = !favorite.value
        }) {
            Icon(imageVector = if (favorite.value) Icons.Filled.FavoriteBorder else Icons.Filled.Favorite, contentDescription = null)
        }
    }
}

@Composable
fun ListImageItem(item: Item, modifier: Modifier) {
    Image(
        painter = painterResource(id = item.imageId), contentDescription = null,
        modifier = modifier
    )
}

@Composable
fun ListLine() {
    Column(
        modifier = Modifier
            .height(1.dp)
            .fillMaxWidth()
            .background(Color.Red)
    ) {}


}

@Composable
@Preview
fun PreviewMyVerticalList() {
    MyVerticalList()
}